// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

using System;
using System.Diagnostics;

namespace FreshMeat.ErrorHandlingBlock
{
   /// <summary>
   /// A retry play that always waits the same amount of time before retrying.
   /// </summary>
   [DebuggerNonUserCode]
   public class FixedRetryPlan : IRetryPlan
   {
      readonly int maxAttempts;
      readonly TimeSpan wait;

      /// <summary>
      /// Creates instance using specified parameters.
      /// </summary>
      /// <param name="maxAttempts">Max attempts.</param>
      /// <param name="wait">Wait between attempts.</param>
      public FixedRetryPlan(int maxAttempts, TimeSpan wait)
      {
         this.maxAttempts = maxAttempts;
         this.wait = wait;
      }

      /// <summary>
      /// Determine whether to retry based on the number of attempts.
      /// </summary>
      /// <param name="attempts">The number of attempts so far, including the initial attempt.</param>
      /// <returns>True to retry, false to fail.</returns>
      public bool ShouldRetry(int attempts)
      {
         return attempts < maxAttempts;
      }

      /// <summary>
      /// Gets the time until the next attempt should be made.
      /// </summary>
      /// <param name="attempts">The number of attempts so far, including the initial attempt.</param>
      /// <returns>Time until the next attempt should be made.</returns>
      public TimeSpan GetTimeUntilNextAttempt(int attempts)
      {
         return wait;
      }
   }
}